#define  _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"

void StackInit(pStack ps)
{
	assert(ps);

	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

void StackPush(pStack ps, STDataType data)
{
	assert(ps);

	if (ps->capacity == ps->top)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* temp = (STDataType*)realloc(ps->arr, newcapacity * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		ps->arr = temp;
		ps->capacity = newcapacity;
	}

	ps->arr[ps->top++] = data;
}

void StackPop(pStack ps)
{
	assert(ps && ps->top);

	ps->top--;
}

bool StackEmpty(pStack ps)
{
	assert(ps);

	return ps->top == 0;
}

STDataType StackTop(pStack ps)
{
	assert(ps && ps->top);

	return ps->arr[ps->top - 1];
}

int StackSize(pStack ps)
{
	assert(ps);

	return ps->top;
}


void StackDestory(pStack ps)
{
	assert(ps);

	free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}